Third Party Control Integration

Use Microsoft's Calendar Control to Pick Dates

Description
This example shows how to use a third-party control to edit a date field value.
Variables
Parent Table
Select a database table
Record Control Class
Select the record control where this customization will be inserted
Date Field Control
Select a date field that interacts with the calendar control
Applies to
RecordControl class
Code
 
''' 
''' Event handler for the init event
'''     
Private Sub myCalendarInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init

    Dim calendar As System.Web.UI.WebControls.Calendar
    calendar = CType(Me.Page.FindControlRecursively("calendar"), System.Web.UI.WebControls.Calendar)            
    AddHandler calendar.SelectionChanged, AddressOf CalendarControl_SelectionChanged

End Sub

''' 
''' CalendarControl_SelectionChanged sets the text of the field value control
''' to the dated selected by the user
'''  
Private Sub CalendarControl_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim calendar As System.Web.UI.WebControls.Calendar
    calendar = CType(Me.Page.FindControlRecursively("calendar"), System.Web.UI.WebControls.Calendar)
    If(Not IsNothing(calendar)) Then
        ${Date Field Control}.Text = calendar.SelectedDate.ToString()
        Dim rec As New ${Parent Table}Record()
        rec.Parse(calendar.SelectedDate.ToString(), ${${Parent Table}ClassName}.${Date Field Control})
        Me.${Date Field Control}.Text = rec.Format(${${Parent Table}ClassName}.${Date Field Control})
    End If

End Sub

''' 
''' Override DataBind(),call mybase Databind() to populate the UI
''' controls using the DataSource 
'''  
Public Overrides Sub DataBind()

    MyBase.DataBind()

    If Not Me.Page.IsPostBack Then

        If Me.DataSource Is Nothing Then
            Return
        End If

        Me.${Date Field Control}.Text = Me.DataSource.Format(${${Parent Table}ClassName}.${Date Field Control})
        if(Me.${Date Field Control}.Text.Length() > 0) Then
        
            Dim dt As Date = Convert.ToDateTime(Me.${Date Field Control}.Text)
            Dim calendar As System.Web.UI.WebControls.Calendar
            calendar = CType(Me.Page.FindControlRecursively("calendar"), System.Web.UI.WebControls.Calendar)

            If (Not IsNothing(calendar)) Then
            
                calendar.SelectedDate = dt
                calendar.VisibleDate = dt
                
            End If
            
        End If

    End If

End Sub

''' 
''' Set the text of the ${Date Field Control} and then call
''' MyBase.SaveData() to  save the data
'''  
Public Overrides Sub SaveData()

    Dim calendar As System.Web.UI.WebControls.Calendar
    calendar = CType(Me.Page.FindControlRecursively("calendar"), System.Web.UI.WebControls.Calendar)    
    If(not isnothing(calendar) AndAlso calendar.SelectedDate.Year <> 1 AndAlso ${Date Field Control}.Text = calendar.SelectedDate.ToString())Then
        ${Date Field Control}.Text = calendar.SelectedDate.ToString()
    End If     
    
    ' Call MyBase.SaveData()
    MyBase.SaveData()

End Sub

     

Terms of Service Privacy Statement